package cn.iocoder.yudao.module.promotion.groupon.api;

import cn.iocoder.yudao.framework.business.basic.pojo.dto.LocationQueryDTO;
import cn.iocoder.yudao.framework.business.basic.pojo.dto.SortQueryDTO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.api.promotion.gift.dto.GiftActivityVO;
import cn.iocoder.yudao.module.api.promotion.groupon.GrouponPackageApi;
import cn.iocoder.yudao.module.api.promotion.groupon.dto.GrouponPackageRespDTO;
import cn.iocoder.yudao.module.api.promotion.groupon.dto.GrouponProductRespDTO;
import cn.iocoder.yudao.module.promotion.gift.service.app.AppGiftActivityService;
import cn.iocoder.yudao.module.promotion.groupon.pojo.dto.AppGrouponPackageQueryDTO;
import cn.iocoder.yudao.module.promotion.groupon.pojo.vo.AppGrouponPackageVO;
import cn.iocoder.yudao.module.promotion.groupon.service.app.AppGrouponPackageService;
import cn.iocoder.yudao.module.promotion.groupon.service.app.AppGrouponProductService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.*;

/**
 * <pre>
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 *  这里是文件说明
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * </pre>
 *
 * @author 山野羡民（1032694760@qq.com）
 * @since 2025/01/13
 */
@Service
@Validated
public class GrouponPackageApiImpl implements GrouponPackageApi {
    @Resource
    private AppGrouponPackageService appGrouponPackageService;
    @Resource
    private AppGrouponProductService appGrouponProductService;
    @Resource
    private AppGiftActivityService appGiftActivityService;

    @Override
    public GrouponPackageRespDTO getDetail(Long id) {
        AppGrouponPackageVO respVO = appGrouponPackageService.getDetail(id);
        respVO.setProducts(appGrouponProductService.getListByPackageId(id));
        respVO.setGiftActivities(appGiftActivityService.getList(respVO.getGiftActivityIds()));
        GrouponPackageRespDTO dto = BeanUtils.toBean(respVO, GrouponPackageRespDTO.class);
        dto.setProducts(BeanUtils.toBean(respVO.getProducts(), GrouponProductRespDTO.class));
        dto.setGiftActivities(BeanUtils.toBean(respVO.getGiftActivities(), GiftActivityVO.class));
        return dto;
    }

    @Override
    public Map<Long, GrouponPackageRespDTO> getMap(Collection<Long> ids) {
        Map<Long, GrouponPackageRespDTO> result = new HashMap<>();
        Map<Long, AppGrouponPackageVO> map = appGrouponPackageService.getMap(ids, AppGrouponPackageVO::getId);
        map.forEach((k, v) -> result.put(k, BeanUtils.toBean(v, GrouponPackageRespDTO.class)));
        return result;
    }

    @Override
    public Map<Long, List<GrouponPackageRespDTO>> getMapByShopIds(Collection<Long> shopIds) {
        return appGrouponPackageService.getMapByShopIds(shopIds, 2);
    }

    @Override
    public Map<Long, Integer> getShopDistanceMap(LocationQueryDTO location, SortQueryDTO sort) {
        return appGrouponPackageService.getShopDistanceMap(location, sort);
    }

    @Override
    public Map<Long, Long> getShopPackageMap(Collection<Long> shopIds) {
        return appGrouponPackageService.getShopPackageMap(shopIds);
    }

    @Override
    public PageResult<GrouponPackageRespDTO> getPage(Integer pageNo, Integer pageSize, Set<Long> shopIds) {
        AppGrouponPackageQueryDTO param = new AppGrouponPackageQueryDTO();
        param.setShopIds(shopIds);
        PageResult<AppGrouponPackageVO> page = appGrouponPackageService.getPage(pageNo, pageSize, param);
        return BeanUtils.toBean(page, GrouponPackageRespDTO.class);
    }

}
